package ${project.group}.${project.module}.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import com.echin.base.dao.impl.SpringDataQueryDaoImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ${project.group}.${project.module}.model.${project.model.model};
import ${project.group}.${project.module}.dao.${project.model.model}Dao;

import ${project.group}.${project.module}.condition.${project.model.model}Condition;



/**
*@Title: ${project.model.model}DaoImpl
*@Description: ${project.model.comment} 数据持久层接口实现
*@author code generator
*@version 1.0
*@date ${date}
*/
@Repository
public class ${project.model.model}DaoImpl extends SpringDataQueryDaoImpl<${project.model.model}> implements ${project.model.model}Dao {
    
    private Log log = LogFactory.getLog(this.getClass());
    
    public ${project.model.model}DaoImpl(){
        super(${project.model.model}.class);
    }
    
    /**
     * 分页查询相关信息，根据传入的bean类对象和分页对象page取得查询结果集List
     * @param model${project.model.model}Condition类
     * @param page 传入的分页对象
     * @return Page<${project.model.model}> 查询的结果集
     * @author code generator
     * @date ${date}
     */
    @Override 
    public Page<${project.model.model}> list(${project.model.model}Condition ${project.model.modelL}Condition,Pageable pageable){
        StringBuffer sb=new StringBuffer("select model from ${project.model.model} model  where 1=1  ");
        Map<String,Object> params = new HashMap<String,Object>();
#foreach($field in $project.fieldList)
#if($field.type=="String")
#if($field.match == 'any')
        if(StringUtils.isNotBlank(${project.model.modelL}Condition.get${field.fieldUp}())){
            sb.append(" and model.${field.field} like :${field.field} ");
            params.put("${field.field}","%"+${project.model.modelL}Condition.get${field.fieldUp}()+"%");
        }
#elseif($field.match == 'eq')
        if(StringUtils.isNotBlank(${project.model.modelL}Condition.get${field.fieldUp}())){
            sb.append(" and model.${field.field} = :${field.field} ");
            params.put("${field.field}",${project.model.modelL}Condition.get${field.fieldUp}());
        }
#elseif($field.match == 'left')
        if(StringUtils.isNotBlank(${project.model.modelL}Condition.get${field.fieldUp}())){
            sb.append(" and model.${field.field} like :${field.field} ");
            params.put("${field.field}",${project.model.modelL}Condition.get${field.fieldUp}()+"%");
        }
#elseif($field.match == 'right')
        if(StringUtils.isNotBlank(${project.model.modelL}Condition.get${field.fieldUp}())){
            sb.append(" and model.${field.field} like :${field.field} ");
            params.put("${field.field}","%"+${project.model.modelL}Condition.get${field.fieldUp}());
        }
#end
#else
#if($field.match != 'none')
        if(${project.model.modelL}Condition.get${field.fieldUp}()!=null){
            sb.append(" and model.${field.field} = :${field.field} ");
            params.put("${field.field}",${project.model.modelL}Condition.get${field.fieldUp}());
        }
#end
#end
#end
        return queryByJpql(sb.toString(), params, pageable);
    }

    @Override 
    public List<${project.model.model}> list(${project.model.model}Condition ${project.model.modelL}Condition){
        StringBuffer sb=new StringBuffer("select model from ${project.model.model} model  where 1=1  ");
        Map<String,Object> params = new HashMap<String,Object>();
#foreach($field in $project.fieldList)
#if($field.type=="String")
#if($field.match == 'any')
        if(StringUtils.isNotBlank(${project.model.modelL}Condition.get${field.fieldUp}())){
            sb.append(" and model.${field.field} like :${field.field} ");
            params.put("${field.field}","%"+${project.model.modelL}Condition.get${field.fieldUp}()+"%");
        }
#elseif($field.match == 'eq')
        if(StringUtils.isNotBlank(${project.model.modelL}Condition.get${field.fieldUp}())){
            sb.append(" and model.${field.field} = :${field.field} ");
            params.put("${field.field}",${project.model.modelL}Condition.get${field.fieldUp}());
        }
#elseif($field.match == 'left')
        if(StringUtils.isNotBlank(${project.model.modelL}Condition.get${field.fieldUp}())){
            sb.append(" and model.${field.field} like :${field.field} ");
            params.put("${field.field}",${project.model.modelL}Condition.get${field.fieldUp}()+"%");
        }
#elseif($field.match == 'right')
        if(StringUtils.isNotBlank(${project.model.modelL}Condition.get${field.fieldUp}())){
            sb.append(" and model.${field.field} like :${field.field} ");
            params.put("${field.field}","%"+${project.model.modelL}Condition.get${field.fieldUp}());
        }
#end
#else
#if($field.match != 'none')
        if(${project.model.modelL}Condition.get${field.fieldUp}()!=null){
            sb.append(" and model.${field.field} = :${field.field} ");
            params.put("${field.field}",${project.model.modelL}Condition.get${field.fieldUp}());
        }
#end
#end
#end
        return queryByJpql(sb.toString(), params);
    }
    
    
    @Override 
    public Page< Map<String,Object>> listBySql(${project.model.model}Condition condition,Pageable pageable){
        StringBuffer sb=new StringBuffer("select #foreach($field in $project.fieldList)#if($!{velocityCount}!=1),#end${field.column}#end from ${project.model.table}  where 1=1  ");
        Map<Integer,Object> params = new HashMap<Integer,Object>();
#foreach($field in $project.fieldList)
#if($field.type=="String")
#if($field.match == 'any')
        if(StringUtils.isNotBlank(condition.get${field.fieldUp}())){
            sb.append(" and ${field.column} like ?$!{velocityCount} ");
            params.put($!{velocityCount},"%"+condition.get${field.fieldUp}()+"%");
        }
#elseif($field.match == 'eq')
        if(StringUtils.isNotBlank(condition.get${field.fieldUp}())){
            sb.append(" and ${field.column} = ?$!{velocityCount} ");
            params.put($!{velocityCount},condition.get${field.fieldUp}());
        }
#elseif($field.match == 'left')
        if(StringUtils.isNotBlank(condition.get${field.fieldUp}())){
            sb.append(" and ${field.column} like ?$!{velocityCount} ");
            params.put($!{velocityCount},condition.get${field.fieldUp}()+"%");
        }
#elseif($field.match == 'right')
        if(StringUtils.isNotBlank(condition.get${field.fieldUp}())){
            sb.append(" and ${field.column} like ?$!{velocityCount} ");
            params.put($!{velocityCount},"%"+condition.get${field.fieldUp}());
        }
#end
#else
#if($field.match != 'none')
        if(condition.get${field.fieldUp}()!=null){
            sb.append(" and ${field.column} = ?$!{velocityCount} ");
            params.put($!{velocityCount},condition.get${field.fieldUp}());
        }
#end
#end
#end
        return queryFieldsBySql(sb.toString(), params, pageable);
    }
    
    
    @Override 
    public ${project.model.model} findBy(String field,Object value){
        StringBuffer sb=new StringBuffer("select model from ${project.model.model} model  where model.");
        sb.append(field);
        sb.append(" = :value ");
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("value",value);
        List<${project.model.model}> result = queryByJpql(sb.toString(), params,0l,1l);
        return result.isEmpty() ? null : result.get(0);
    }
    
    @Override 
    public List<${project.model.model}> findAllBy(String field,Object value){
        StringBuffer sb=new StringBuffer("select model from ${project.model.model} model  where model.");
        sb.append(field);
       sb.append(" = :value ");
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("value",value);
        return queryByJpql(sb.toString(), params);
    }
}